var sfzhW = [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2];
var sfzhProv = ['11','12','13','14','15','21','22','23','31','32','33','34','35','36','37','41','42','43','44','45','46','50','51','52','53','54','61','62','63','64','65'];

function convertTo18(id){
    if (id==null || id.length !=15)
        return id;  //若为空或长度不等于15位,原样返回
    var idOrigin=id;
    if (id.length ==15) 
       id=id.substring(0,6)+"19"+id.substring(6,15);
    var regStr1 = /^(\d){17}$/;
    if(!regStr1.test(id)){
        return idOrigin;
    }
    var num=0;
    for (var i=0;i<17;i++) {
        num+=sfzhW[i]*(id.charAt(i)-'0');
    }
    num=num%11;
    var xc="10X98765432";
    if (id.length ==17) {
        id=id+xc.charAt(num); 
    }
    return id.toUpperCase();
}

function isSfzh(id) {
    if (id==null) return false;
    if (id.length ==15 && isSfzh15(id)){
        id = convertTo18(id);
    }
    if (id.length != 18) {
        return false;
    }
    var regStr1 = /^(\d){17}([\d|X|x])$/;
    if(!regStr1.test(id)){
        return false;
    }
    if (id.charAt(17)=='x') 
           id=id.substring(0,17)+'X';
    var num=0;
    for (var i=0;i<17;i++) {
        num+=sfzhW[i]*(id.charAt(i)-'0');
    }
    num=num%11;
    var xc="10X98765432";
    if (xc.charAt(num) != id.charAt(17))
        return false;
    else 
       return true;
}

function isSfzh15(id){
    if (id.length != 15) {
        return false;
    }
    var regStr = /^\d{15}$/;
    if(!regStr.test(id)){
          return false;
    }
    var prov = id.substring(0,2);
    if ($.inArray(prov, sfzhProv) == -1) {
        return false;
    }
    var year=parseInt($(id.substring(6, 8)).val());
    var month=parseInt($(id.substring(8, 10)).val());
    var day=parseInt($(id.substring(10, 12)).val());
    if ( (year < 0 || year > 99) 
            || (month < 1 || month > 12)
            || (day < 1 || day > 31) )
        return false;
    return true;
}

$(function(){           
     formValidate('user_reg_fm','newbutton');
});
/**
 * 注册页面表单验证（邮箱注册、手机号注册、企业注册）
 * @param  {string} fromId 表单id
 * @param  {string} btnId  按钮Id
 * @return {string}        null
 */
function formValidate(fromId,btnId){
    var focusMessage = {
        name:"请输入您的姓名",
        tel: "请输入您的联系方式",
        sex: "请选择您的性别。",
        birthday :"请填写您的出生年月",
        experience: "请选择培训经历",
        trainyear: "请选择培训年份",
        club : "请填写您目前的俱乐部。",
        credentialtype : "请选择赛事经历。",
        otherperson : "请填写紧急联系人。",
        othertel: "请填写紧急联系人联系方式。"
    };
    var errorIcon = '<i class="iconfont" title="提示">&#xe67d;</i>';
    $("#" + fromId).validate({
        errorElement: "span",
        onkeyup: function (element) {
            return false;
        },
        onfocusin: function (element) {
            /*var $this = $(element);
            $this.parent('td').next('.validate-tips').text(focusMessage[$this.attr("id")]);
            //$this.removeClass('has-error');*/
            var $this = $(element);
            $this.removeClass('has-error');
            var msg = focusMessage[$this.attr("id")] || '';
            if (msg != '') {
                msg = '<span>' + msg + '</span>';
                $this.parent('td').next('td.validate-tips').html(msg);
            }
        },
        rules: {
            name: {
                required: true,
            },
            tel: {
                required: true,
                maxlength: 20
            },
            sex: {
                required: true
            },
            birthday: {
                required: true
            }
           
           
        },
        errorClass: "error",
        errorPlacement : function(error, element) {
            var $this = $(element);
            var $tips = $this.parent('td').next('td.validate-tips');
            $tips.html('');
            error.appendTo($tips);
        },
        success : function(label) {
            var $error = $(label);
            $error.html('<i class="iconfont color2ea" title="成功">&#xe67e;</i>');
            $error.parents('tr').find('.has-error').removeClass("has-error"); 
        },
        highlight: function ( element, errorClass, validClass ) {
            var $input = $( element );
            if(!$input.hasClass("has-error")){
                $input.addClass("has-error" ).removeClass( "has-success" );
            }
        },
        messages : {
            name:{
                required: errorIcon + "请输入您的姓名！"
            },
            tel : {
                required: errorIcon + "请输入您的联系方式！"
            },
            birthday: {
                required: errorIcon + "请输入您的出生年月！"
            }
        }
    });
}
var mphoneRemoteRole = {
    url: "/account/checkmobilephoneother.action",
    async: false,
    type:"post",
    data:{
        dataInfo:function(){
            return $("#mphone").val();
        },
        optType:function(){
            return "REGISTER";
        }
    }
};
$.validator.addMethod("sfzh", function(value, element, param){
    var sfzValue = $("#sfzh").val();
    if($("#credentialtype").val() == 'SFZ'){
        if(!isSfzh(sfzValue)){
            return false;
        }
        if( $("#credentialtype").val() == 'SFZ' && !isSfzh(sfzValue)){
              $("#" + $(element).attr("id")+ "_tip_msg_area1").show();
            $("#" + $(element).attr("id") + "_tip_msg").text("身份证号不规范");
        }
    }
    return true;
},"身份证号格式有误！");

$.validator.addMethod("mphone", function(value/*数据值*/, element, param/*正则表达式*/){
    var regStr = /^((13\d{9})|(14\d{9})|(15\d{9})|(166\d{8})|(173\d{8})|(175\d{8})|(176\d{8})|(177\d{8})|(178\d{8})|(18\d{9})|(198\d{8})|(199\d{8}))$/;
    return regStr.test(value);
},"手机号格式不正确！");

$.validator.addMethod("vcode", function(value, element, param){
    var validateNumber_v = $('#vcode').val();
    if(validateNumber_v.length != 6){
        return false;
    }
    return true;
},"短信验证码不正确！");
$.validator.addMethod("xm", function(value, element, param){
    var regStr_ = /(^[\u4e00-\u9fa5]+([\.\·][\u4e00-\u9fa5]+)*$)|(^[a-zA-Z]+([\s|\.|\-][a-zA-Z]+)*$)|(^[a-zA-Z]+[\u4e00-\u9fa5]+([\u4e00-\u9fa5]|[a-zA-Z])*$)|(^[\u4e00-\u9fa5]+[a-zA-Z]+([\u4e00-\u9fa5]|[a-zA-Z])*$)/;
    var v_ = $("#xm").val();
    if(!regStr_.test(v_)){
        return false;
    }
    return true;
},"姓名格式有误！");
$.validator.addMethod("strongPsw",function(value,element){
    if($().checkPasswordSecLevel(value)==1){
        return true;
    }else{
        return false;
    }
},'<i class="iconfont" title="提示">&#xe67d;</i>密码安全性太低(需包含数字、小写字母、大写字母、特殊字符中的两种及以上)！');